﻿/**San Antonio Health Center Patient Information Management System
 * 
 * Author: Monica DM. Tosino
 * Title: Index/Login GUI
 * Date Finished: February 18, 2010
 **/

/**
 * ===================================================================================
 *  Log In v2.
 *  Edited by: Nikko Fernando O. Santillan
 *  Date Finished: February 21, 2010
 *  ----------------------------------------------------------------------------------
 *  
 *  Version Updates:
 *  -log in page linked to create account
 *  -log in page linked to homePage
 *  -fixed the url of accounts database (from static address to dynamic address)
 *  
 * ===================================================================================
 **/


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace sahcpims
{
    public partial class logIn : Form
    {
        public logIn()
        {
            InitializeComponent();
                
        }

        public int empNum;
        public string empName;
    
        /****** THE MAIN EVENT HANDLER ******/
        private void loginButton_Click(object sender, EventArgs e)
        {
            /* Checks if the username and password fields are filled */
            if (this.USERNAMEfield.Text == "" || this.PASSWORDfield.Text == "")
            {
                this.errorlabel.Text = "Login Field(s) Empty."; //prompts the user that there are empty fields
            }
            else
            {
                /** transfer the values of the form to a local variable for easier access **/
                string username = this.USERNAMEfield.Text;
                string pword = this.PASSWORDfield.Text;

                /** Connection String: Provider of the connection and the source of data **/
                string conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\accounts.mdb";

                /** create instance of the connection with the connection string as parameter **/
                OleDbConnection conn = new OleDbConnection(conString);

                /** 
                 * Creating an instance of the DataSet which represents an in-memory cache of data
                 * This is where the result of the database query will be stored
                 **/
                DataSet ds = new DataSet();

                /** Opens the Connection to the database **/
                conn.Open();

                /** 
                 * Creating an instance that will represent a set of data commands and database connection 
                 * that are used to fill the System.Data.DataSet
                **/
                OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * from accounts", conn);

                /** The query result in the adapter is transferred in ds **/
                adapter.Fill(ds);

                /** closes the connection to the database **/
                conn.Close();

                string flag = ""; // flag that will be used to determine if the username and the password exist on the same row

                /** creating an instance of DataTable that represents one table of in-memory data **/
                DataTable dt = ds.Tables[0];

                /** "Traverses" the dt DataTable by row to find the username and password supplied by the user **/
                foreach (DataRow dr in dt.Rows)
                {
                    /** Checking the row if it holds both the username and the password entered by the user **/
                    if (dr["User"].ToString() == username && dr["Pass"].ToString() == pword)
                    {
                        empName = dr["Empname"].ToString();
                        empNum = int.Parse(dr["EmpNum"].ToString());
                        flag = "Logged"; //"mark" the flag as "Logged"
                        break; // force exit the foreach loop
                    }
                }

                /** 
                 * If the flag is not set to "Logged", 
                 * then the username and the password do not exist on the same row in the database 
                 **/
                if (flag != "Logged")
                {
                    this.errorlabel.Text = "Incorrect Login"; //user is prompted that the login info is incorrect
                }
                else
                {
                    //invoke the home page
                    //close this window
                    homePage home = new homePage();
                    home.homeEmpName = empName;
                    home.homeEmpNum = empNum;
                    home.putAccountInfo(); //put info to homePage

                    this.Visible = false;
                    home.Visible = true;
                }

            }
        }

        /******** EVENT HANDLER FOR THE CREATE ACCOUNT LINK ********/
        private void createLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //invoke the create account window
            //close this window
            createAccount create = new createAccount();
            this.Visible = false;
            create.Visible = true;
            
        }

    }
}

